/*
 * Copyright (c) 2022 Queclink-C-Library
 *
 * This file is subject to the terms and conditions of the MIT License.
 * For details, please see the LICENSE file or visit the MIT website: https://mit-license.org/
 */

#ifndef QLIBC_UTILS_H
#define QLIBC_UTILS_H

#ifdef __cplusplus
extern "C" {
#endif

/**
 * @addtogroup utils
 * @ingroup common
 * @brief Declare by `#include <qlibc/utils.h>`. Some utility macros and functions.
 * @{
 */

#if !defined(ARRAY_COUNT) || defined(DOXYGEN)
#define ARRAY_COUNT(array)      (sizeof(array) / sizeof(array[0]))      /**< Counts the number of elements in an array */
#endif /* ARRAY_COUNT */

#if !defined(SHIFT) || defined(DOXYGEN)
#define SHIFT(bit)      (1ul << (bit))      /**< Set the corresponding bit */
#endif /* SHIFT */

#if !defined(set_bit) || defined(DOXYGEN)
#define set_bit(word,bit)       (word) |= (1ul << (bit))        /**< Set the `bit` of `word` */
#endif /* set_bit */

#if !defined(get_bit) || defined(DOXYGEN)
#define get_bit(word,bit)       ((0 != ((word) & (1ul << (bit)))) ? 1 : 0)      /**< Get the `bit` of `word` */
#endif /* get_bit */

#if !defined(clr_bit) || defined(DOXYGEN)
#define clr_bit(word,bit)       (word) &= ~(1ul << (bit))       /**< Clear the `bit` of `word` */
#endif /* clr_bit */

/**
 * @brief   Calculate the distance between two locations on the map.
 * @param[in] lng1 The longitude of the first location.
 * @param[in] lat1 The latitude of the first location.
 * @param[in] lng2 The longitude of the second location.
 * @param[in] lat2 The latitude of the second location.
 * @return  The distance between two locations on the map, in km.
 * @authors Hubert.Lee, Jason.Huang
 * @date    2022/12/06
 */
double quec_mapdist(double lng1, double lat1, double lng2, double lat2);

/** utils
 * @}
 */

#ifdef __cplusplus
}
#endif

#endif /* QLIBC_UTILS_H */
